package com.ruoyi.file.service;


import com.aliyun.oss.OSS;
import com.aliyun.oss.model.OSSObject;
import com.ruoyi.file.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * 阿里云oss
 */
@Service
public class OssSysFileServiceImpl implements ISysFileService {

    // 创建OSSClient实例。
    @Resource
    OSS ossClient;

    @Value("${spring.cloud.alicloud.oss.bucket}")
    private String bucketName;

    @Value("${spring.cloud.alicloud.oss.basepath}")
    private String basePath;

    @Override
    public String uploadFile(MultipartFile file) throws Exception {
        // 创建PutObject请求。
        String url = basePath + FileUploadUtils.extractFilename(file); // 服务器目标上传地址
        ossClient.putObject(bucketName, url, file.getInputStream());


        System.out.println("上传成功！");
        //ossClient.shutdown();
        return url;
    }


    public InputStream downloadFile(String filePath) throws FileNotFoundException {
        //下载
        OSSObject ossObject = ossClient.getObject(bucketName, filePath);
        InputStream fileInputStream = ossObject.getObjectContent();
        return fileInputStream;
    }

}
